using System;
using Microsoft.Win32;

namespace Widgetsphere.Core.Util
{
  /// <summary>
  /// Summary description for RegistryHelper.
  /// </summary>
  public partial class RegistryHelper
  {
    private RegistryHelper()
    {
    }

    public static void SetLocalMachineRegistryValue(string path, string item, string newValue)
    {
			RegistryKey key = null;
			try
			{
				key = Registry.LocalMachine.OpenSubKey(path, true);
				if (key == null)
					key = Registry.LocalMachine.CreateSubKey(path);
				if (key != null)
					key.SetValue(item, newValue, RegistryValueKind.String);
			}
			finally
			{
				if (key != null)
					key.Close();
			}

    }

    public static string GetLocalMachineRegistryValue(string path, string item)
    {
      RegistryKey key = null;
      string returnVal = null;
      try
      {
        key = Registry.LocalMachine.OpenSubKey(path);
        if (key != null)
        {
          returnVal = (string)key.GetValue(item);
        }
      }
      catch
      {
      }
      finally
      {
				if (key != null)
					key.Close();

      }
      return returnVal;
    }

    public static string GetLocalMachineRegistryValue(string path, string item, string defaultString)
    {
      string retVal = GetLocalMachineRegistryValue(path, item);
      if (retVal == null)
      {
        retVal = defaultString;
      }
      return retVal;
    }

    public static void SetCurrentUserRegistryValue(string path, string item, string newValue)
    {
			RegistryKey key = null;
			try
			{
				key = Registry.CurrentUser.OpenSubKey(path, true);
				if (key == null)
					key = Registry.CurrentUser.CreateSubKey(path);
				if (key != null)
					key.SetValue(item, newValue, RegistryValueKind.String);
			}
			finally
			{
				if (key != null)
					key.Close();
			}
    }

    public static string GetCurrentUserRegistryValue(string path, string item)
    {
      RegistryKey key = null;
      string returnVal = string.Empty;
      try
      {
        key = Registry.CurrentUser.OpenSubKey(path);
        if (key != null)
        {
          returnVal = (string)key.GetValue(item);
        }
      }
      catch
      {
      }
      finally
      {
				if (key != null)
					key.Close();

      }
      return returnVal;
    }

    public static string GetCurrentUserRegistryValue(string path, string item, string defaultString)
    {
      string retVal = GetCurrentUserRegistryValue(path, item);
      if((retVal == null) || (retVal == string.Empty))
      {
        retVal = defaultString;
      }
      return retVal;
    }

  }
}
